from flask import g, request, jsonify, current_app
from app.common.result import trueReturn, falseReturn
from functools import wraps
import traceback

api_URL = 'https://sms_developer.zhenzikj.com'
app_id = 108557
app_secret = 'a7bad66f-18e6-481b-9553-5639f9be1f40'


def handle_error(func):
    @wraps(func)
    def decorator(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except:
            return falseReturn(None, traceback.format_exc())

    return decorator


def smart_decorator(decorator):
    def decorator_proxy(func=None, **kwargs):
        if func is not None:
            return decorator(func=func, **kwargs)

        def decorator_proxy(func):
            return decorator(func=func, **kwargs)

        return decorator_proxy

    return decorator_proxy


@smart_decorator
def verify_params(func, params=[]):
    @wraps(func)
    def decorator(*args, **kwargs):
        for param in params:
            if not g.data or param not in g.data:
                return falseReturn(None, "缺少参数:{}".format(param))
        return func(*args, **kwargs)

    return decorator
